package jdbc;

import java.sql.*;
import java.util.Scanner;
/**
 * 账号登陆
 */
public class jog {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        System.out.println("帐号登录");
        System.out.println("请输入账号");
        String i =sc.next();
        System.out.println("请输入密码");
        String  j = sc.next();
        jog jo = new jog();
        jo.log_1(i,j);
    }
    public void log_1(String user,String password){
        Connection conn = null;
        PreparedStatement pre = null;
        boolean b = true;
            try {
                //获取链接
                conn=util.JDBCUtils.getConnection();
                conn.setAutoCommit(false);
                //定义SQL
                String sql = "select *from tb_admin where user=? and password=?";
                //建立链接
                pre=conn.prepareStatement(sql);
                //给指定参数设置值
                pre.setString(1,"sun");
                pre.setString(2,"123");
                //执行给定SQL
                b=pre.execute();
                if( b){
                    System.out.println("登陆成功！");
                }else {
                    System.out.println("登陆失败！");
                }
                //提交事务
                conn.commit();
            } catch (Exception throwables) {
                try {
                    if(conn!=null){
                    //回滚
                    conn.rollback();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                throwables.printStackTrace();
            }finally {
                //关流
                util.JDBCUtils.close(conn,pre);
            }
        }
}
